﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using NthParameterSeries.PriceServiceReference;
using System.Collections.ObjectModel;
using System.Linq;
using System.ComponentModel;


namespace NthParameterSeries.Models
{
    public class PortfolioMaintenanceViewModel : IPortfolioMaintenanceViewModel,    INotifyPropertyChanged
    {
        IList<Portfolio> portfolioList = new List<Portfolio>();
        IList<Country> countryList = new List<Country>();
        IList<City> cityList = new List<City>();

        private ObservableCollection<Portfolio> portfolios;
        private ObservableCollection<Country> countries;
        private ObservableCollection<City> cities;
        private Portfolio portfolio;
        private Portfolio currentPortfolioItem;

        public ObservableCollection<Portfolio> Portfolios { get { return portfolios; } }
        public ObservableCollection<Country> Countries { get { return countries; } }
        public ObservableCollection<City> Cities { get { return cities; } }

        public Portfolio Portfolio
        {
            get { return portfolio; }
            set
            {
                if (portfolio == value) return;
                portfolio = value;
                NotifyPropertyChanged("Portfolio");
            }
        }


         #region INotifyPropertyChanged Members

         public event PropertyChangedEventHandler PropertyChanged;

         protected void NotifyPropertyChanged(string propertyName)
         {
             if (PropertyChanged != null)
                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }
         #endregion

       
        private readonly PriceServiceClient service;


        public PortfolioMaintenanceViewModel()
        {

            service = new PriceServiceClient();

            portfolios = new ObservableCollection<Portfolio>(portfolioList);
            countries = new ObservableCollection<Country>(countryList);
            cities = new ObservableCollection<City>(cityList);

            service.GetCountriesCompleted += new EventHandler<GetCountriesCompletedEventArgs>(service_GetCountriesCompleted);
            service.GetCountriesAsync();

            service.GetCitiesCompleted += new EventHandler<GetCitiesCompletedEventArgs>(service_GetCitiesCompleted);
            service.GetCitiesAsync();
           
            service.GetPortfoliosCompleted += new EventHandler<GetPortfoliosCompletedEventArgs>(service_GetPortfolioCompleted);
            service.GetPortfoliosAsync();

            service.PutPortfoliosCompleted += new EventHandler<AsyncCompletedEventArgs>(service_PutPortfoliosCompleted);

        }

        void service_GetCitiesCompleted(object sender, GetCitiesCompletedEventArgs e)
        {
            e.Result.ToList<City>().ForEach(x => cities.Add(x));
        }

        void service_GetCountriesCompleted(object sender, GetCountriesCompletedEventArgs e)
        {
            e.Result.ToList<Country>().ForEach(x => countries.Add(x));
            
        }

        

        void service_GetPortfolioCompleted(object sender, GetPortfoliosCompletedEventArgs e)
        {
            e.Result.ToList<Portfolio>().ForEach(x => portfolios.Add(x));
            CurrentPortfolioItem = portfolios.FirstOrDefault<Portfolio>();
            
        }

       
        public String Name
        {
            set { if (CurrentPortfolioItem != null && !String.IsNullOrEmpty(value) && CurrentPortfolioItem.Name != value) CurrentPortfolioItem.Name = value; }
            get {
                if (CurrentPortfolioItem == null) 
                return "Empty";
                return CurrentPortfolioItem.Name;
            }
        }

        //public String CountryName
        //{
        //    set { //if (CurrentPortfolioItem != null && value != null && CurrentPortfolioItem.Country != value) 
        //       // CurrentPortfolioItem.Country = value;
        //       //NotifyPropertyChanged("Country");
        //    }
        //    get
        //    {
        //        if (CurrentPortfolioItem == null)
        //            return string.Empty;
        //        return "countries.Where(x => x.Id == CurrentPortfolioItem.CountryId.Value).SingleOrDefault().Name";
        //    }
        //}

        public Location Address
        {
            set { if (CurrentPortfolioItem != null && value != null 
                && (CurrentPortfolioItem.Address.City != value.City || CurrentPortfolioItem.Address.Country != value.Country)) 
                CurrentPortfolioItem.Address = value; }
            get
            {
                if (CurrentPortfolioItem == null)
                    return default(Location);
                return CurrentPortfolioItem.Address;
            }
        }

        public int? CountryId
        {
            set
            {
                if (CurrentPortfolioItem != null && value != null
                    && CurrentPortfolioItem.CountryId != value)

                {
                    CurrentPortfolioItem.CountryId = value;
                    NotifyPropertyChanged("CountryId");
                }
            }
            get
            {
                if (CurrentPortfolioItem == null)
                    return default(int?);
                return CurrentPortfolioItem.CountryId;
            }
        }

        public int? CityId
        {
            set
            {
                if (CurrentPortfolioItem != null && value != null
                    && CurrentPortfolioItem.CityId != value)
                {
                    CurrentPortfolioItem.CityId = value;
                    NotifyPropertyChanged("CityId");
                }
            }
            get
            {
                if (CurrentPortfolioItem == null)
                    return default(int?);
                return CurrentPortfolioItem.CityId;
            }
        }

        public DateTime InceptionDate
        {
            set { if (CurrentPortfolioItem != null && value != null && CurrentPortfolioItem.InceptionDate != value) CurrentPortfolioItem.InceptionDate = value; }
            get
            {
                if (CurrentPortfolioItem == null)
                    return DateTime.Now;
                return CurrentPortfolioItem.InceptionDate;
            }
        }

        
        public Portfolio CurrentPortfolioItem
        {

            get { return currentPortfolioItem; }
            set
            {
                if (currentPortfolioItem == value) return;
                currentPortfolioItem = value;
                Name = currentPortfolioItem.Name;
                NotifyPropertyChanged("CurrentPortfolioItem");
                NotifyPropertyChanged("Name");
                NotifyPropertyChanged("Address");
                NotifyPropertyChanged("CityId");
                NotifyPropertyChanged("CountryId");
                NotifyPropertyChanged("InceptionDate");
                
            }
        }


        public void AddNewItem()
        {
            var newPortfolio = new Portfolio() { Name = "New Portfolio", Address = new Location()};
            Portfolios.Add(newPortfolio);
            CurrentPortfolioItem = newPortfolio;
        }

        public void Save()
        {
            ObservableCollection<Portfolio> changedPortfolios = new ObservableCollection<PriceServiceReference.Portfolio>();
            Portfolios.Where(x => x.Id == 0).ToList().ForEach(y => changedPortfolios.Add(y));
            service.PutPortfoliosAsync(Portfolios);
        }

        void service_PutPortfoliosCompleted(object sender, AsyncCompletedEventArgs e)
        {
            portfolios.Clear();
            service.GetPortfoliosAsync();
        }
    }
}
